package com.mes.common.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.context.ContextHolder;
import com.mes.basicdata.dao.EmployeeDao;
import com.mes.basicdata.domain.Employee;
import com.mes.system.dao.EmployeeRoleDao;

@Component
public class SysUtil {

	@Autowired
	private ConfigInfo configInfo;
	@Autowired
	private EmployeeDao employeeDao;
	@Resource
	private EmployeeRoleDao employeeRoleDao;

	private static final Log logger = LogFactory.getLog(SysUtil.class);
	
	// 原来是MMUtils中的变量声明，暂时放过来还没想好要不要改 author lcs
	public static final ThreadLocal<MMSystemSession> systemSessionLocal = new ThreadLocal<MMSystemSession>();
	// 原来是MMUtils中的变量声明用于getExecutorService
	// 便于对方案内的线程进行管理
	private static ExecutorService executorService;
	
	/**
	 * 通过系统登录的人员employeeUid获取EmployeeRole对象，然后根据EmployeeRole对象获取角色
	 * @return
	 * @throws Exception
	 */
	public List<String> getSessionRoleId() throws Exception {
		
		String employeeUid = ContextHolder.getLoginUser().getUsername();
		
		List<String> roleIdList = employeeRoleDao.getEmployeeRoleByEmployeeUid(employeeUid);
		
		if(roleIdList != null) return roleIdList;
		return null;
	}
	
	
	/**
	 * @author lcs
	 * @see 用于schedule中获取ExecutorService，原来是MMUtils中方法
	 * 如果涉及到数据库连接池，最好不要用此方法 会造成连接风暴 控制在15个线程
	 * @return
	 */
	public static ExecutorService getExecutorService() {
		if (executorService == null) {
			// executorService = Executors.newCachedThreadPool();
			// 固定占有连接池
			executorService = Executors.newFixedThreadPool(30);
		}
		return executorService;
	}
	
	
	
	/**
	 * 读取在系统Properties文件中配置的参数
	 * 
	 * @param name
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public String getProperty(String name) throws Exception {
		Class<?> clazz = Class.forName("com.mes.common.util.ConfigInfo");
		String[] sss = StringUtils.split(name, ".");
		// name = name.substring(0, 1).toUpperCase() + name.substring(1);
		name = "";
		for (int i = 0; i < sss.length; i++) {
			name = name + sss[i].substring(0, 1).toUpperCase() + sss[i].substring(1);
		}
		Method method = clazz.getMethod("get" + name);
		String propertyValue = (String) method.invoke(configInfo);
		return propertyValue;
	}

	/**
	 * 获取系统唯一号
	 * 
	 * @return
	 */
	public static String getUUID() {
		return IdentityGenerator.GetIdentityUUID();
	}

	/**
	 * 获取当前登录的人员唯一号
	 * 
	 * @return
	 */
	public static String getSessionEmployeeUid() {
		return ContextHolder.getLoginUser().getUsername();
	}

	/**
	 * 获取当前登录人员的默认部门唯一号
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getSessionDeptUid() throws Exception {
		String employeeUid = ContextHolder.getLoginUser().getUsername();
		Employee employee = employeeDao.getById(employeeUid);
		
		return employee.getDeptUid();
	}


	/**
	 * @author lcs
	 * @see 主要是用于schedule中manager判断部门是否存在原来是MMUtils中方法
	 * @param deptUid
	 * @param propertyName
	 * @return
	 * @throws Exception
	 */
	public boolean isDeptExist(String deptUid, String propertyName) throws Exception {
		boolean isDeptExist = false;
		try {
			String strDept = getProperty(propertyName);
			if (strDept != null && strDept.length() > 0) {
				String[] aDept = strDept.split(",");
				for (String str : aDept) {
					if (deptUid.startsWith(str)) {
						isDeptExist = true;
						break;
					}
				}
			}
		} catch (RuntimeException e) {
			logger.error("123", e);
		}
		return isDeptExist;
	}

}
